//
//  ViewControllerForClickCell.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewControllerForClickCell.h"
#import "CellForClickYouJiCell.h"
#import "NetworkingHandler.h"
#import "ModelOfClickShouYeCell.h"
#import "HeaderViewForClickCell.h"
#import "HeaderOfEverySectionForClickCell.h"
#import "ModelOfTripDays.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


#import "CellForClickYouJiCell2.h"
#import "ModelForCollectDescription.h"
#import "ModelForCollectPhoto.h"
#import "ModelForSpecificContents.h"

#import <CommonCrypto/CommonDigest.h>
#import "Reachability.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewControllerForClickCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIView *viewDaoHangLan;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrayForTripDays;
@property (nonatomic, retain)UIView *viewOfHeader;
@property (nonatomic, retain)UIImageView *imageViewOfHeader;
@property (nonatomic, retain)ModelOfClickShouYeCell *model;
@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)UIButton *buttonOfRightFenXiang;

@property (nonatomic, retain)NSMutableArray *arrayForSpecific;
@property (nonatomic, retain)NSMutableArray *arrayForNotes;
@property (nonatomic, retain)NSMutableArray *arrayForPhoto;
@property (nonatomic, retain)NSMutableArray *arrayForDescription;
@property (nonatomic, retain)NSMutableArray *arrayForDic;



@end

@implementation ViewControllerForClickCell

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_buttonOfLeft release];
    [_buttonOfRightFenXiang release];
    [_imageViewOfHeader release];
    [_viewOfHeader release];
    [_arrayForTripDays release];
    [_id release];
    [_tableView release];
    [_viewDaoHangLan release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self panDuanWangLuo];
    
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.arrayForTripDays = [NSMutableArray array];
    
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil];
    self.navigationController.navigationBar.translucent = YES;
    
    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    /** 解析数据 */
    [self handleData];
    
    /** 创建tableView */
    [self createTableView];
}

#pragma mark - 判断网络状态
- (void)panDuanWangLuo {
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:{
            isExistenceNetwork = NO;
            NSLog(@"`````````网络不给力");
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前无网络" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            
            [alert addAction:actionOK];
            
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
            break;
        }
        case ReachableViaWiFi:{
            isExistenceNetwork = YES;
            break;
        }
        case ReachableViaWWAN:{
            isExistenceNetwork = YES;
            break;
        }
    }
    
}

#pragma mark - 解析数据
- (void)handleData {
    
    NSString *string = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/%@.json", self.id];
    
    self.arrayForSpecific = [NSMutableArray array];
    self.arrayForNotes = [NSMutableArray array];
    self.arrayForPhoto = [NSMutableArray array];
    self.arrayForDescription = [NSMutableArray array];
    
    [NetworkingHandler networkingWithURL:string completeHandle:^(NSData *data,id result) {
        
        self.model = [[ModelOfClickShouYeCell alloc] init];
        self.model.name = [result objectForKey:@"name"];
        self.model.front_cover_photo_url = [result objectForKey:@"front_cover_photo_url"];
        self.model.start_date = [result objectForKey:@"start_date"];
        self.model.photos_count = [result objectForKey:@"photos_count"];
        self.model.user = [result objectForKey:@"user"];
        self.model.trip_days = [result objectForKey:@"trip_days"];
  
        [_model release];
        
        NSArray *array = [result objectForKey:@"trip_days"];
        
        for (NSDictionary *dic in array) {
            
            ModelForSpecificContents *model = [[ModelForSpecificContents alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayForSpecific addObject:model];
            
            [model release];
            
            for (NSDictionary *dic2 in [dic objectForKey:@"nodes"]) {
                
                for (NSDictionary *dic3 in [dic2 objectForKey:@"notes"]) {
                    
                    ModelForCollectDescription *modelwen = [[ModelForCollectDescription alloc] init];
                    
                    [modelwen setValuesForKeysWithDictionary:dic3];
                    
                    [self.arrayForDescription addObject:modelwen];
                    
                    [modelwen release];
                    
                    ModelForCollectPhoto *modeltu = [[ModelForCollectPhoto alloc] init];
                    
                    [modeltu setValuesForKeysWithDictionary:[dic3 objectForKey:@"photo"]];
                    
                    [self.arrayForPhoto addObject:modeltu];
                   
                    [modeltu release];
                    
                }
            }
        }
        [self.tableView reloadData];

    }];
}

#pragma mark - 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.tableView registerClass:[HeaderViewForClickCell class] forHeaderFooterViewReuseIdentifier:@"pool1"];
    [self.tableView registerClass:[HeaderOfEverySectionForClickCell class] forHeaderFooterViewReuseIdentifier:@"pool2"];
    
    [self.tableView registerClass:[CellForClickYouJiCell class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[CellForClickYouJiCell2 class] forCellReuseIdentifier:@"pool7"];
    
}

#pragma mark - tableView协议方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        HeaderViewForClickCell *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"pool1"];
        [header passModel:self.model];
        
        return header;
    } else {

        return nil;
    }
    
}

/** 分区头部高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 180;
        
    } else {
        
        return 0;
    }
}

/** 返回分区数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
  
    return 2;
}

/** 每行cell的高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelForCollectDescription *modelForDescription = self.arrayForDescription[indexPath.row];
    
    ModelForCollectPhoto *modelForPhoto = self.arrayForPhoto[indexPath.row];
    
    if (modelForPhoto.image_height != 0) {
        
        CellForClickYouJiCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool7"];
        
        return [cell heightForCellWithDescription:modelForDescription.descrip width:modelForPhoto.image_width height:modelForPhoto.image_height];
        
    } else {
        
        CellForClickYouJiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        return [cell heightForCellWithDescription:modelForDescription.descrip];
        
    }
    
    
  
}

/** 返回每个分区的row数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
        
    } else {
        
        return self.arrayForDescription.count;
    }
    
}

/** 返回cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelForCollectDescription *modelForDescription = self.arrayForDescription[indexPath.row];
    
    ModelForCollectPhoto *modelForPhoto = self.arrayForPhoto[indexPath.row];
    
    if (modelForPhoto.image_height != 0) {
        
        CellForClickYouJiCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool7"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

        [cell voluationToCellSubViewsWithString:modelForDescription.descrip photo:modelForPhoto.url width:modelForPhoto.image_width height:modelForPhoto.image_height];
        
        
        return cell;
    } else {
        
        CellForClickYouJiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

        [cell voluationToCellSubViewsWithString:modelForDescription.descrip];
        
        
        return cell;
    }
 
    
}

#pragma mark - 左上角返回按钮
- (void)fanHui:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
